package com.excesys.exsecs.om.job;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.excesys.exsecs.common.security.service.ExsecsUser;
import com.excesys.exsecs.common.security.util.SecurityUtils;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.entity.OmRepairPlan;
import com.excesys.exsecs.om.entity.OmWEvent;
import com.excesys.exsecs.om.entity.OmWOrder;
import com.excesys.exsecs.om.entity.OmWOrderHistory;
import com.excesys.exsecs.om.mapper.OmWEventMapper;
import com.excesys.exsecs.om.mapper.OmWOrderHistoryMapper;
import com.excesys.exsecs.om.mapper.OmWOrderMapper;
import com.excesys.exsecs.om.mapper.SequenceMapper;
import com.excesys.exsecs.om.service.OmRepairPlanService;
import com.excesys.exsecs.om.utils.DateUtils;
import com.excesys.exsecs.om.utils.OrderNoUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class RepairPlanToRepairPlanJob {

	private final OmRepairPlanService omRepairPlanService;

	private final SequenceMapper sequenceMapper;

	private final OrderNoUtils orderNoUtils;

	private final OmWOrderHistoryMapper omWOrderHistoryMapper;

	private final OmWOrderMapper omWOrderMapper;

	private final OmWEventMapper omWEventMapper;

	@Scheduled(cron = "0 0 0 * * ? ")
	public void createSheetJob() throws ParseException {
		log.info("设备维修计划批处理开始。。。。。");
		List<OmRepairPlan> list = omRepairPlanService.getList();
		log.info("list:" + list);
		for (OmRepairPlan omRepairPlan : list) {
			// 定期计划
			if ("0".equals(omRepairPlan.getPlanType())) {
				// 周期计划 0天/1月
				if ("0".equals(omRepairPlan.getPlanCycleType())) {
					Date d = new Date();
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String date = format.format(d);
					log.info("现在的日期是：" + date);
					int day = Integer.valueOf(omRepairPlan.getFrequency());
					int size = omRepairPlan.getExecuteNum();
					String lastday = omRepairPlan.getExcuteDate();
					Calendar ca = Calendar.getInstance();
					ca.setTime(format.parse(lastday));
					ca.add(Calendar.DATE, day * size);
					d = ca.getTime();
					String backTime = format.format(d);
					log.info("增加天数以后的日期：" + backTime);
					if (timeCalendars(
							new SimpleDateFormat("yyyy-MM-dd")
									.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
							new SimpleDateFormat("yyyy-MM-dd").parse(backTime))) {
						// 0-工单/1-事件
						if ("0".equals(omRepairPlan.getPlanFlag())) {
							// 生成工单号
							int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
							OmWOrder omWOrder = new OmWOrder();
							omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
							omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
							omWOrder.setOrderFrom("5");
							DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
							omWOrder.setApplyTime(
									LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
							omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
							OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
							omWOrderHistory.setOperation("创建工单");
							ExsecsUser ExsecsUser = SecurityUtils.getUser();
							// 工单报告人信息
							// omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
							// omWOrder.setOrderUserId(ExsecsUser.getId());
							// omWOrder.setOrderUserName(ExsecsUser.getName());
							// omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
							omWOrder.setRepairType("1");
							omWOrder.setEquipNo(omRepairPlan.getDeviceCode());
							omWOrder.setEquipName(omRepairPlan.getDeviceName());
							// omWOrderHistory.setOperateUserId(ExsecsUser.getId());
							// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
							omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
							omWOrderHistory.setEventNo(omWOrder.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistory);
							SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
							// 生成事件
							int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
							OmWEvent omWEvent = new OmWEvent();
							omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
							omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
							OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
							omWOrderHistorys.setOperation("创建事件");
							ExsecsUser ExsecsUsers = SecurityUtils.getUser();
							// omWOrderHistorys.setOperateUserId(ExsecsUsers.getId());
							// omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
							omWOrderHistorys.setEventNo(omWEvent.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistorys);
							omWEvent.setRepairType("1");
							omWEvent.setEventStatus("3");
							// omWEvent.setEventUserId(ExsecsUsers.getId());
							// omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
							omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
							SqlHelper.retBool(omWEventMapper.insert(omWEvent));
						}
						else {
							int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
							OmWEvent omWEvent = new OmWEvent();
							omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
							omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
							OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
							omWOrderHistory.setOperation("创建事件");
							ExsecsUser ExsecsUser = SecurityUtils.getUser();
							// omWOrderHistory.setOperateUserId(ExsecsUser.getId());
							// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
							omWOrderHistory.setEventNo(omWEvent.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistory);
							omWEvent.setRepairType("1");
							omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
							// omWEvent.setEventUserId(ExsecsUser.getId());
							// omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
							SqlHelper.retBool(omWEventMapper.insert(omWEvent));
						}
					}
					omRepairPlan.setExecuteNum(omRepairPlan.getExecuteNum() + 1);
					omRepairPlanService.updateById(omRepairPlan);
				}
				else {
					String size = omRepairPlan.getFrequency();
					String lastday = omRepairPlan.getPlanStartTime().substring(5, 7);
					if (1 == ((Integer.valueOf(lastday) + Integer.valueOf(size)) + "").length()) {
						lastday = "0" + lastday;
					}
					log.info("增加天数以后的日期：" + lastday);
					String n[] = omRepairPlan.getPlanStartTime().split("-");
					String c = n[0] + "-0" + ((Integer.valueOf(omRepairPlan.getPlanStartTime().substring(5, 7))
							+ Integer.valueOf(size)) + "") + "-" + n[2];
					boolean flag = timeCalendars(
							new SimpleDateFormat("yyyy-MM-dd")
									.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
							new SimpleDateFormat("yyyy-MM-dd").parse(c));
					if (flag) {
						// 0-工单/1-事件
						if ("0".equals(omRepairPlan.getPlanFlag())) {
							// 生成工单号
							int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
							OmWOrder omWOrder = new OmWOrder();
							omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
							omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
							omWOrder.setOrderFrom("5");
							DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
							omWOrder.setApplyTime(
									LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
							omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
							OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
							omWOrderHistory.setOperation("系统生成维修工单");
							// ExsecsUser ExsecsUser = SecurityUtils.getUser();
							// 工单报告人信息
							// omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
							// omWOrder.setOrderUserId(ExsecsUser.getId());
							omWOrder.setOrderUserName("系统生成");
							// omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
							omWOrder.setRepairType("1");
							omWOrder.setEquipNo(omRepairPlan.getDeviceCode());
							omWOrder.setEquipName(omRepairPlan.getDeviceName());
							omWOrderHistory.setOperateUserId(1L);
							// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
							omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
							omWOrderHistory.setEventNo(omWOrder.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistory);
							SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
							// 生成事件
							int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
							OmWEvent omWEvent = new OmWEvent();
							omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
							omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
							OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
							omWOrderHistorys.setOperation("系统生成维修事件");
							// ExsecsUser ExsecsUsers = SecurityUtils.getUser();
							omWOrderHistorys.setOperateUserId(1L);
							// omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
							omWOrderHistorys.setEventNo(omWEvent.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistorys);
							omWEvent.setRepairType("1");
							omWEvent.setEventStatus("3");
							omWEvent.setEventUserName("维修计划系统生成");
							omWEvent.setSourceOfEvent("2");
							// omWEvent.setEventUserId(ExsecsUsers.getId());
							// omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
							omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
							SqlHelper.retBool(omWEventMapper.insert(omWEvent));
						}
						else {
							int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
							OmWEvent omWEvent = new OmWEvent();
							omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
							omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
							OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
							omWOrderHistory.setOperation("系统生成维修事件");
							// ExsecsUser ExsecsUser = SecurityUtils.getUser();
							// omWOrderHistory.setOperateUserId(ExsecsUser.getId());
							// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
							omWOrderHistory.setEventNo(omWEvent.getEventNo());
							omWOrderHistoryMapper.insert(omWOrderHistory);
							omWEvent.setRepairType("1");
							omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
							// omWEvent.setEventUserId(ExsecsUser.getId());
							// omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
							SqlHelper.retBool(omWEventMapper.insert(omWEvent));
						}
					}
					omRepairPlan.setExecuteNum(omRepairPlan.getExecuteNum() + 1);
					omRepairPlanService.updateById(omRepairPlan);
				}
			}
			else {
				// 临期计划
				boolean flag = timeCalendars(
						new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
						new SimpleDateFormat("yyyy-MM-dd").parse(omRepairPlan.getPlanStartTime()));
				if (flag) {
					// 0-工单/1-事件
					if ("0".equals(omRepairPlan.getPlanFlag())) {
						// 生成工单号
						int orderSeq = sequenceMapper.getSeqNextVal("orderseq");
						OmWOrder omWOrder = new OmWOrder();
						omWOrder.setOrderNo(orderNoUtils.getOrderNumber("Order", orderSeq));
						omWOrder.setOrderName("维修计划-" + omWOrder.getOrderNo());
						omWOrder.setOrderFrom("5");
						DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A);
						omWOrder.setApplyTime(LocalDateTime.parse(LocalDateTime.now().format(formatter), formatter));
						omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_0);// 待指派状态
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("系统生成维修工单");
						// ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// 工单报告人信息
						// omWOrder.setOrderUserDeptId(ExsecsUser.getDeptId());
						// omWOrder.setOrderUserId(ExsecsUser.getId());
						omWOrder.setOrderUserName("系统生成");
						// omWOrder.setOrderUserPhone(ExsecsUser.getPhone());
						omWOrder.setRepairType("1");
						omWOrder.setEquipNo(omRepairPlan.getDeviceCode());
						omWOrder.setEquipName(omRepairPlan.getDeviceName());
						omWOrderHistory.setOperateUserId(1L);
						// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setOrderNo(omWOrder.getOrderNo());
						omWOrderHistory.setEventNo(omWOrder.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						SqlHelper.retBool(omWOrderMapper.insert(omWOrder));
						// 生成事件
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistorys = new OmWOrderHistory();
						omWOrderHistorys.setOperation("系统生成维修事件");
						// ExsecsUser ExsecsUsers = SecurityUtils.getUser();
						omWOrderHistorys.setOperateUserId(1L);
						// omWOrderHistorys.setOperateDeptId(ExsecsUsers.getDeptId());
						omWOrderHistorys.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistorys);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus("3");
						omWEvent.setEventUserName("维修计划系统生成");
						omWEvent.setSourceOfEvent("2");
						// omWEvent.setEventUserId(ExsecsUsers.getId());
						// omWEvent.setEventUserDeptId(ExsecsUsers.getDeptId());
						omWEvent.setAssociateTickets(orderNoUtils.getOrderNumber("Order", orderSeq));
						SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					else {
						int eventSeq = sequenceMapper.getSeqNextVal("eventseq");
						OmWEvent omWEvent = new OmWEvent();
						omWEvent.setEventNo(orderNoUtils.getOrderNumber("Event", eventSeq));
						omWEvent.setEventName("维修计划-" + omWEvent.getEventNo());
						OmWOrderHistory omWOrderHistory = new OmWOrderHistory();
						omWOrderHistory.setOperation("系统生成维修事件");
						// ExsecsUser ExsecsUser = SecurityUtils.getUser();
						// omWOrderHistory.setOperateUserId(ExsecsUser.getId());
						// omWOrderHistory.setOperateDeptId(ExsecsUser.getDeptId());
						omWOrderHistory.setEventNo(omWEvent.getEventNo());
						omWOrderHistoryMapper.insert(omWOrderHistory);
						omWEvent.setRepairType("1");
						omWEvent.setEventStatus(OmCommonConstants.EVENT_STATUS_NO_USER);
						// omWEvent.setEventUserId(ExsecsUser.getId());
						// omWEvent.setEventUserDeptId(ExsecsUser.getDeptId());
						SqlHelper.retBool(omWEventMapper.insert(omWEvent));
					}
					omRepairPlan.setExecuteNum(omRepairPlan.getExecuteNum() + 1);
					omRepairPlanService.updateById(omRepairPlan);
				}
			}
		}

		log.info("设备维修计划批处理结束。。。。。");

	}

	public static boolean timeCalendars(Date nowTime, Date endTime) {
		if (nowTime.getTime() == endTime.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}

	public static boolean timeCalendar(Date nowTime, Date endTime) {
		// 设置当前时间
		Long startTime = nowTime.getTime();
		// 设置结束时间
		Long endTimes = endTime.getTime();
		// 处于开始时间之后，和结束时间之前的判断
		if (endTimes.compareTo(startTime) >= 0) {
			return true;
		}
		else {
			return false;
		}
	}

	public static void main(String[] args) {
		String a = "2023-05-29";
		String b = "2";
		System.out.println(a.substring(5, 7));
		System.out.println(((Integer.valueOf(a.substring(5, 7)) + Integer.valueOf(b)) + "").length());

		System.out.println("0" + ((Integer.valueOf(a.substring(5, 7)) + Integer.valueOf(b)) + ""));

		String n[] = a.split("-");
		String c = n[0] + "-0" + ((Integer.valueOf(a.substring(5, 7)) + Integer.valueOf(b)) + "") + "-" + n[2];
		System.out.println(c);
		// for(int i=0;i<n.length;i++){
		// String c =
		// n[0]+"-0"+((Integer.valueOf(a.substring(5,7))+Integer.valueOf(b))+"")+"-"+n[2];
		// System.out.println(
		// c
		// );
		// }

	}

}
